package dao.Implementation;

import java.util.ArrayList;
import java.util.List;

import org.hibernate.Session;
import org.hibernate.Transaction;

import Donnees.Animation;
import Util.HibernateUtil;
import dao.Interfaces.IAnimationDAO;

public class AnimationDAO implements IAnimationDAO {
	public AnimationDAO() {
	}

	public List<Animation> selectAll() {
		List<Animation> listeAnimation = new ArrayList<Animation>();

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		listeAnimation.addAll(session.createQuery("from Animation").list());

		session.flush();
		tx.commit();
		session.close();

		return listeAnimation;
	}

	public long insert(Animation a) {
		Animation animationTemp = a;
		Long msgId;

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		msgId = (Long) session.save(animationTemp);

		session.flush();
		tx.commit();
		session.close();

		return msgId;
	}

	public int delete(Animation a) {
		Animation animationTemp = a;
		int retour;

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		retour = session.createQuery(
				"delete from Animation where id='"
						+ animationTemp.getId() + "'").executeUpdate();
		
		session.flush();
		tx.commit();
		session.close();

		return retour;
	}

	public void update(Animation a) {
		Animation animationTemp = a;

		Session session = HibernateUtil.getSessionFactory().openSession();
		Transaction tx = session.beginTransaction();

		session.update(animationTemp);

		session.flush();
		tx.commit();
		session.close();
	}
}
